package com.suian.springbootmysql.service.impl;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.suian.springbootmysql.dao.LoginDao;
import com.suian.springbootmysql.dao.UserDao;
import com.suian.springbootmysql.pojo.ResultVo;
import com.suian.springbootmysql.pojo.TbUser;
import com.suian.springbootmysql.service.LoginService;
import com.suian.springbootmysql.utils.AilCodeUtils;
import net.sf.jsqlparser.statement.select.KSQLWindow;
import org.omg.CORBA.TIMEOUT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginDao loginDao;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private UserDao userDao;
    //发送短信验证码
    @Override
    public ResultVo sendCode(String phone) throws Exception{
        //判断手机号正确
     Boolean  isPhone= Validator.isMobile(phone);
     if (isPhone){
         //判断手机号注册
       int count= loginDao.getUserCountByPhone(phone);
       if (count>0){
            //手机号已经注册
           //发送短信验证码
           AilCodeUtils ailCodeUtils=new AilCodeUtils();
           ailCodeUtils.sendLoginCode(phone,"123455");
           stringRedisTemplate.opsForValue().set(phone,"123455");
           stringRedisTemplate.expire(phone,5, TimeUnit.MINUTES);
           return new ResultVo(200,"短信发送成功",null);
       }else {
           return new ResultVo(303,"手机号码错误",null);
       }
     }else {
         return new ResultVo(303,"手机号码错误",null);
     }
    }

    @Override
    public ResultVo userLogin(String phone, String code) {
      Boolean isHavephone= StrUtil.isEmpty(phone);
        boolean isHaveCode = StringUtils.isEmpty(code);
        if (!isHavephone){
            if (!isHaveCode){
                LambdaQueryWrapper<TbUser> lambdaQueryWrapper=new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(TbUser::getPhone,phone);
                TbUser  tbUser=userDao.selectOne(lambdaQueryWrapper);
                int count=loginDao.getUserCountByPhone(phone);
                    if (tbUser!=null){
                        String redisCode=stringRedisTemplate.opsForValue().get(phone);
                        if (!StringUtils.isEmpty(redisCode)){
                                if (redisCode.equals(code)){
                                    return new ResultVo(200,"success",null);
                                }else {
                                    return new ResultVo(303,"验证码输入错误",null);

                                }
                        }else {
                            return new ResultVo(303,"验证码过期",null);

                        }
                    }else {
                        return new ResultVo(303,"用户不存在",null);
                    }
            }else {
                return new ResultVo(303,"验证码必须填写",null);
            }
        }
        else {
            return new ResultVo(303,"手机号码必须填写",null);
        }
    }
}
